<?php
namespace App\Service\Admin\Qudao;

use App\Service\Admin\BaseService as Service;

class SucaiService extends Service
{
    //素材

    /**
     * 列表
     */
    public function getList()
    {
        $db = $this->db2Conn($this->dbQdSucai)->where('del', 0);
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            //商品
            $gOne = (array) $this->db2Conn($this->dbQdGoods)
                ->where('id', $v['gid'])
                ->first();
            $gname = empty($gOne) ? '-' : $gOne['title'];
            $gimg = ''; $gPrefix = ''; $gno = '';
            if (!empty($gOne)) {
                if (file_exists($gOne['bigimg'])) {
                    $gPrefix = env('DOMAIN') . '/';
                } else {
                    $gPrefix = env('FC_DOMAIN') . '/';
                }
                $gimg = $gOne['bigimg'];
                $gno = $gOne['gno'];
            }
            //图片
            $imgOne = (array) $this->db2Conn($this->dbQdScimg)
                ->where('dmid', $v['id'])
                ->orderBy('id','asc')
                ->first();
            if (empty($imgOne)) {
                $imgPrefix = '';
                $imgLink = '';
            } else {
                if (file_exists($imgOne['link'])) {
                    $imgPrefix = env('DOMAIN') . '/';
                } else {
                    $imgPrefix = env('FC_DOMAIN') . '/';
                }
                $imgLink = $imgOne['link'];
            }
            $arys[] = [
                'id' => $v['id'],
                'gno' => $gno,
                'gname' => $gname,
                'gPrefix' => $gPrefix,
                'gimg' => $gimg,
                'title' => $v['title'],
                'detail' => $v['detail'],
                'imgPrefix' => $imgPrefix,
                'imgLink' => $imgLink,
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     */
    public function getOne($id)
    {
        $res = (array) $this->db2Conn($this->dbQdSucai)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!count($res)) {
            $imgList = [];
            $imgOnes = $this->db2Conn($this->dbQdScimg)
                ->where('dmid', 0)
                ->get();
            if (count($imgOnes)) {
                foreach ($imgOnes as $imgOne) {
                    $imgOne = (array) $imgOne;
                    $imgPrefix = '';
                    $imgLink = '';
                    if (!empty($imgOne)) {
                        if (file_exists($imgOne['link'])) {
                            $imgPrefix = env('DOMAIN') . '/';
                        } else {
                            $imgPrefix = env('FC_DOMAIN') . '/';
                        }
                        $imgLink = $imgOne['link'];
                    }
                    $imgList[] = [
                        'imgPrefix' => $imgPrefix,
                        'imgLink' => $imgLink,
                    ];
                }
            }
            $arys = [
                'id' => 0, 'gno' => '', 'gname' => '',
                'gPrefix' => '', 'gimg' => '',
                'title' => '', 'detail' => '',
                'imgList' => $imgList,
                'uptime' => '',
            ];
            return $this->toback(0, '', $arys);
        }
        //商品
        $gOne = (array) $this->db2Conn($this->dbQdGoods)
            ->where('id', $res['gid'])
            ->first();
        $gname = empty($gOne) ? '-' : $gOne['title'];
        $gimg = ''; $gPrefix = ''; $gno = '';
        if (!empty($gOne)) {
            if (file_exists($gOne['bigimg'])) {
                $gPrefix = env('DOMAIN') . '/';
            } else {
                $gPrefix = env('FC_DOMAIN') . '/';
            }
            $gimg = $gOne['bigimg'];
            $gno = $gOne['gno'];
        }
        //图片
        $imgList = [];
        $imgOnes = $this->db2Conn($this->dbQdScimg)
            ->where('dmid', $res['id'])
            ->get();
        if (count($imgOnes)) {
            foreach ($imgOnes as $imgOne) {
                $imgOne = (array) $imgOne;
                $imgPrefix = '';
                $imgLink = '';
                if (!empty($imgOne)) {
                    if (file_exists($imgOne['link'])) {
                        $imgPrefix = env('DOMAIN') . '/';
                    } else {
                        $imgPrefix = env('FC_DOMAIN') . '/';
                    }
                    $imgLink = $imgOne['link'];
                }
                $imgList[] = [
                    'imgPrefix' => $imgPrefix,
                    'imgLink' => $imgLink,
                ];
            }
        }
        $arys = [
            'id' => $res['id'],
            'gno' => $gno,
            'gname' => $gname,
            'gPrefix' => $gPrefix,
            'gimg' => $gimg,
            'title' => $res['title'],
            'detail' => $res['detail'],
            'imgList' => $imgList,
            'uptime' => $res['uptime'],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 添加
     * @param string $gno
     * @param array $pas
     */
    public function add($gno, $pas)
    {
        if ($gno) {
            $gOne = (array) $this->db2Conn($this->dbQdGoods)->where('gno', $gno)->first();
            if (empty($gOne)) {
                return $this->toback(-2, '没有该商品');
            }
            $pas['gid'] = $gOne['id'];
        } else {
            $pas['gid'] = 0;
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->db2Conn($this->dbQdSucai)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        $this->db2Conn($this->dbQdScimg)->where('dmid', 0)->update(['dmid' => $lastid]);
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id
     * @param string $gno
     * @param array $pas
     */
    public function modify($id, $gno, $pas)
    {
        $res = (array) $this->db2Conn($this->dbQdSucai)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        if ($gno) {
            $gOne = (array) $this->db2Conn($this->dbQdGoods)->where('gno', $gno)->first();
            if (empty($gOne)) {
                return $this->toback(-2, '没有该商品');
            }
            if ($res['gid'] != $gOne['id']) {
                $pas['gid'] = $gOne['id'];
            }
        }
        $pas['uptime'] = toDate();
        $up = $this->db2Conn($this->dbQdSucai)->where('id', $id)
            ->update($pas);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        $this->db2Conn($this->dbQdScimg)->where('dmid', 0)->update(['dmid' => $id]);
        return $this->toback();
    }

    /**
     * 添加图片
     * @param int $scid
     * @param string $img 图片
     * @param int $w 宽度
     * @param int $h 高度
     */
    public function addImg($scid, $img, $w, $h)
    {
        //素材库
        $mdData = [
            'dmid' => $scid, 'link' => $img,
            'width' => $w, 'height' => $h,
            'addtime' => toDate(),
        ];
        $lastid = $this->db2Conn($this->dbQdScimg)->insertGetId($mdData);
        if (!$lastid) {
            return $this->toback(-1, '素材图片失败');
        }
        //图库
        $imgData = [
            'link' => $img,
            'width' => $w,
            'height' => $h,
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbImg)->insertGetId($imgData);
        if (!$lastid) {
            return $this->toback(-1, '图库失败');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function setDel($id)
    {
        $up = $this->db2Conn($this->dbQdSucai)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}